Search the web for text selected in Vim
This tip allows you to search the web for a selected phrase from Vim. This is especially useful to lookup spellings of proper nouns, phrases, function prototypes and man pages on the web, while working in Vim. Usage: *Add the vmap ?? line given below to your vimrc. *In Vim press v to begin selection. *move cursor to end of selection (region will be highlighted). *Press ?? *The search results will appear in your web browser. :vmap ?? :silent exec \ ":!c:/opera/6*/opera.exe \\\"http://www.google.com/search?q=".substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") \ . "\\\" " The vmap takes the visually selected region, and removes all non word characters and single characters in region, and launches the query on the phrase. You should replace c:/opera/6*/opera.exe by path to your browser, or you can get the opera from http://www.opera.com for windows/linux, opera is super fast and safe in textmode with images and java turned off. You can replace the vmap selection by above to query for . Comments Do you have too many backslashes? You may consider using !start also. ---- The backslashes are because of my shell setting in vim (I have sh.exe): if has("win32") set shell=sh shellslash shellcmdflag=-c shellxquote=\" shellpipe=\|\ tee endif cmd.exe will need lesser backslashes. ---- I just looked up the quoting syntax of cmd.exe on win2k, the quoting syntax is very limited but this works on windows: if &shell =~ "cmd" vmap ?? :exec \ ":!c:/opera/61/opera.exe http://www.google.com/search?q=\"" \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") \ . "\"" else :vmap ?? :silent exec \ ":!c:/opera/61/opera.exe \\\"http://www.google.com/search?q="; \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") \ . "\\\" " endif A less flexible but simpler method (search for word under the cursor) nmap ,g :silent !start c:\progra~1\opera75\opera.exe http://www.google.com/search?q=; ---- As pointed out, a 'start' is needed to launch opera in background. This works with sh and cmd.exe: vmap ?? :silent exec \ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\"" \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") \ . "\"" ---- You can use single-quote to lessen the backslash: vmap ?? :exec \ ':!c:/opera/61/opera.exe http://www.google.com/search?q=";' \ . substitute(@*,'\W\+\\|\<\w\>'," ","g") \ . '"' ---- Finally this works on vim63 / Xterm / Linux with Opera 8.0 also. if $TERM =~ "xterm" vmap ?? y:silent exec \ ":!/usr/bin/opera http://www.google.com/search?q='"; \ . substitute(@","\\W\\+\\\\|\\<\\w\\>",'\\%20',"g") \ . "' &" else vmap ?? :silent exec \ ":!start c:/opera/6/opera.exe http://www.google.com/search?q=\"" \ . substitute(@*,"\\W\\+\\\\|\\<\\w\\>"," ","g") \ . "\"" endif I still had trouble with all that slashville, so I did another QAD solution. vmap ,g "zy:let @z = substitute(@z,'space:','+','g') \ :silent !start c:\progra~1\opera75\opera.exe \ http://www.google.com/search?q=z; Explanation: "zy # yank visual area to register z let..substitute # turn spaces into + (wot Google wants) silent..opera.exe # where my opera is (BTW Opera is really kool!) z # recall contents of z You can de-uglify your maps a little by putting your browser path into a VIM variable let $opera="c:\progra~1\opera75\opera.exe" ---- As an extension to this for definitions and spellings, I included this as well as the mapping for a google search: vmap ?w :exec \ ':!/usr/bin/mozilla http://dictionary.reference.com/search?q="' \ . substitute(@*,'\W\+\\|\<\w\>'," ","g") \ . '"' ---- I modified this a bit. Instead of entering Visual mode, selecting the word, and then launching the search, I wanted to just search directly from Normal mode. Additionally, I wanted to launch the default system browser, regardless of product or path. Note: This probably only works under Windows. Once I have a chance to test under Linux, I'll see if I can hack out something cross-platform. map ?g "zyiw \ :exec ':silent ! start http://www.google.com/search?q=";'.@z.'"' This takes the current word under the cursor, saves it to "z, then launches google silently in the default browser passing @z as the search parameter. Very clean, very simple. map ?m "zyiw \ :exec ':silent ! start http://www.m-w.com/cgi-bin/dictionary?va=";'.@z.'"' This does essentially the same, but looks up the word in the Merriam-Webster dictionary instead of Google. ----